package com.tengchao.books.service.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.cassandra.core.CassandraOperations;
import org.springframework.stereotype.Service;

import com.datastax.driver.core.querybuilder.QueryBuilder;
import com.datastax.driver.core.querybuilder.Select;
import com.tengchao.books.exception.DataNotFoundException;
import com.tengchao.books.model.Book;
import com.tengchao.books.model.BookMapper;
import com.tengchao.books.service.BookService;

// ?check what is inside (), type or id
@Service("bookService")
public class BookServiceImpl implements BookService{
	
	private Logger LOGGER = Logger.getLogger(BookServiceImpl.class);
	
	// ? is this needed 
	@Autowired
	// bind with specific id
	@Qualifier("cassandraTemplate")
	private CassandraOperations cassandraOps;

	public Book getBook(int bookid) {
		LOGGER.info("enter into BookServiceImpl: getBook");
		
		Select s = QueryBuilder.select().from("books");
		s.where(QueryBuilder.eq("bookid", bookid));
		
		LOGGER.info("CQL: " + s.toString());
		
		Book book = null;
		
		try{
			// to retrieve object, use RowMapper interface
			book = cassandraOps.queryForObject(s, new BookMapper());
			LOGGER.info(book);
		} catch (IllegalArgumentException e){
			LOGGER.info("NULL book");
			throw new DataNotFoundException("No book found for book id: " + bookid);
		}
		
		
		return book;
	}

	public void createBook(Book book) {
		// TODO Auto-generated method stub
		
	}

	public void deleteBook(int id) {
		// TODO Auto-generated method stub
		
	}

	public void updateBook(int id, Book book) {
		// TODO Auto-generated method stub
		
	}

	public List<Book> getBooks(int offset, int limit) {
		// TODO Auto-generated method stub
		return null;
	}

}
